package app.kotlin_1.activity

import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction
import android.view.View
import app.kotlin_1.R
import app.kotlin_1.base.BaseActivity
import app.kotlin_1.fragment.*
import app.kotlin_1.fragment.HomeFragment.Companion.newIntance
import app.kotlin_1.utils.toast
import com.ashokvarma.bottomnavigation.BottomNavigationBar
import com.ashokvarma.bottomnavigation.BottomNavigationItem
import kotlinx.android.synthetic.main.activity_main.*

/**
 * Created by 刘智强 on 2017/9/19.
 */
class MainActivity : BaseActivity(), BottomNavigationBar.OnTabSelectedListener {
    private var homeFragment: HomeFragment? = null
    private var productFragment: ProductFragment? = null
    private var activityFragment: ActivityFragment? = null
    private var myFragment: MyFragment? = null
    private  var time:Long=0L

    override fun getData() {
    }

    override fun getlayoutId(): Int {
        return R.layout.activity_main
    }


    override fun initView() {
    }

    override fun initData() {
        bottom_bar!!.addItem(BottomNavigationItem(R.drawable.home, "首页"))
                .addItem(BottomNavigationItem(R.drawable.product, "产品"))
                .addItem(BottomNavigationItem(R.drawable.activity, "活动"))
                .addItem(BottomNavigationItem(R.drawable.my, "我的"))
                .setFirstSelectedPosition(0)
                .initialise()
        setDefaultItem()


    }

    private fun setDefaultItem() {
        val fm: FragmentManager? = supportFragmentManager
        val ft: FragmentTransaction? = fm?.beginTransaction()
        ft?.add(R.id.fl_fragment, newIntance("首页"))
        ft?.commit()

    }

    override fun initListenter() {
        bottom_bar.setTabSelectedListener(this)


    }

    override fun requestData() {
    }

    override fun setData(any: Any) {
    }

    override fun setOnClick(v: View?) {
    }

    override fun onTabReselected(p0: Int) {
    }

    override fun onTabUnselected(p0: Int) {
    }

    override fun onTabSelected(p0: Int) {
        val fm = supportFragmentManager
        val ft = fm.beginTransaction()
        hideAllFragment(ft)
        when (p0) {
            0 -> {
                if (homeFragment == null) {
                    homeFragment = HomeFragment.newIntance("首页")
                    ft.add(R.id.fl_fragment, homeFragment)
                } else {
                    ft.show(homeFragment)
                }

            }
            1 -> {
                if (productFragment == null) {
                    productFragment = ProductFragment.newInstance("产品")
                    ft.add(R.id.fl_fragment, productFragment)
                } else {
                    ft.show(productFragment)

                }

            }
            2 -> {
                if (activityFragment == null) {
                    activityFragment = ActivityFragment.newInstance("活动")
                    ft.add(R.id.fl_fragment, activityFragment)

                } else {
                    ft.show(activityFragment)
                }

            }
            3 -> {
                if (myFragment == null) {
                    myFragment = MyFragment()
                    ft.add(R.id.fl_fragment, myFragment)
                } else {
                    ft.show(myFragment)

                }

            }
        }
        ft.commitAllowingStateLoss()
    }

    private fun hideAllFragment(transaction: FragmentTransaction) {
        supportFragmentManager?.fragments?.forEach { transaction?.hide(it!!) }
    }

    override fun onBackPressed() {
        if(System.currentTimeMillis()-time>2000){
            toast("再按一次退出程序")
            time=System.currentTimeMillis()
        }else{
            System.exit(0)
        }

    }

}




